/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import java.util.Vector;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;
import edu.yonsei.iwb.sdt.util.UtilFunctions;

/**
 *
 * @author JDK
 */
public class DisjunctionRefinement extends Refinement {

    private String name = new String();
    private ExpandingHistory eh;
    private String corename = new String();
    private String conditionr;
    private String mateConditionr;

    public DisjunctionRefinement(String name, String corename, ExpandingHistory eh) {
        this.name = name;
        this.eh = eh;
        this.corename = corename;
        super.refinementType = RefinementType.DISJUNCTION;
    }

    public DisjunctionRefinement(DisjunctionRefinement dr) {
        this.name = dr.getName();
        this.eh = dr.getExpandingHistory();
        this.corename = dr.getCoreName();
        this.conditionr = dr.getConditionr();
        this.mateConditionr = dr.getMateConditionr();
        super.refinementType = RefinementType.DISJUNCTION;
    }

    public DisjunctionRefinement(String prefix, String cen, String con) {
        this.name = prefix + cen + DLConstructors.DISJUNCTION + con;
        this.corename = prefix + cen + DLConstructors.DISJUNCTION + con;
        this.cc = new ConstraintConstructor(cen, prefix, con);

        this.conditionr = cen;
        this.mateConditionr = con;
        super.refinementType = RefinementType.DISJUNCTION;
    }

    public DisjunctionRefinement(String prefix, ExpandingHistory eh, String cen, String con) {
        this.eh = eh;
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();

        this.name = cen+ DLConstructors.DISJUNCTION + con;
        this.corename = cen + DLConstructors.DISJUNCTION + con;
        for (int i = 0; i < propertySet.size(); i++) {
            PropertyArc pa = propertySet.get(i);
            String p = pa.getProperty();
            String q = quantifiers.get(i);
            if (pa.getDirection()) {
                this.name = q + " " + p + ".(" + this.name + ")";
            } else {
                this.name = q + " " + p + "￣.(" + this.name + ")";
            }
        }

        this.name = prefix + this.name;
        this.cc = new ConstraintConstructor(cen, prefix, con);
        this.conditionr = cen;
        this.mateConditionr = con;
        super.refinementType = RefinementType.DISJUNCTION;

    }

    public String getName() {
        return name;
    }

    public String getConditionr() {
        return conditionr;
    }

    public String getMateConditionr() {
        return mateConditionr;
    }

    @Override
    public ExpandingHistory getExpandingHistory() {
        return this.eh;
    }

    @Override
    public void generateRefinementName() {
        super.refinement_name = this.name;
    }

    @Override
    public Refinement generateUpdatedRefinement(Vector<TargetInstance> instanceSet) {
        Refinement ref = new DisjunctionRefinement(this);
        ref.setBranchingType(this.branching_type);
        ref.setConstraintConstructor(this.cc);
        Vector<TargetInstance> oldinsSet = this.getInstanceSet();
        for (int i = 0; i < instanceSet.size(); i++) {
            TargetInstance ti = instanceSet.get(i);
            if (oldinsSet.contains(ti)) {
                ref.setInstanceSet(ti);
            }
        }
//        private boolean refinement_type = true;
//        protected String refinement_name;
//        private Vector<Resource> instanceSet;
        return ref;
    }

    @Override
    public String getCoreName() {
        return this.corename;
    }

    @Override
    public Vector<Resource> getConstraintMatcherInstance(Model model) {
         Vector<Resource> css = new Vector<Resource>();
        
        ResIterator cenres = model.listResourcesWithProperty(RDF.type, this.conditionr);
        ResIterator conres = model.listResourcesWithProperty(RDF.type, this.mateConditionr);
        
        Vector<Resource> cen = UtilFunctions.getSubjectResourceVector(cenres);
        Vector<Resource> con = UtilFunctions.getSubjectResourceVector(conres);

        for (int i = 0; i < cen.size(); i++) {
                if(!css.contains(cen.get(i))){
                    css.add(cen.get(i));
                }
        }
        
        for (int i = 0; i < con.size(); i++) {
            if(!css.contains(con.get(i))){
                    css.add(con.get(i));
                }
        }

        return css;
    }
}
